<?php
require('bot/conf.php');
class mini_bot {
	protected $modulesinfo;
	private function parse($file) {
		$file = file_get_contents($file);
		$dzialanie = substr($file,strpos($file,'/**')+3,strpos($file,'*/')-strpos($file,'/**')-4);
		$modreg_start = '//*module_register_start';
		$modreg_end = '//*module_register_end';
		$func_start = '//*function_start';
		$func_end = '//*function_end';
		$ret['module_register'] = trim(substr(
											 $file, 
											 ( (substr_count($file, $modreg_start))?
											   (strpos($file, $modreg_start)+strlen($modreg_start)):
											   strlen($file) ),
											 ( (substr_count($file, $modreg_end))?
											   (strpos($file, $modreg_end)-strpos($file, $modreg_start)-strlen($modreg_end."\n\r"))
											   : ( (substr_count($file, $func_start))?
											       (strpos($file, $func_start)-strpos($file, $modreg_start)-strlen($modreg_start."\n\r"))
											       : null) ) ) );
		$ret['func'] = substr(
							 $file,
							 strpos($file, $func_start)+strlen($func_start),
							 ( (substr_count($file, $func_end))?
							 	( strpos($file, $func_end)-strpos($file, $func_start)-strlen($func_end."\n\r") )
							 	: (strrpos($file, "\n")-strpos($file, $func_start)-1)));
		if (strlen($ret['func'])<2) return 0;
	//	debug("*****!!!This is IT '".nonl($ret['func']).'\' *************', DBG_ERROR);
		$dzialanie = explode('*', str_replace("\n\r","",$dzialanie));
		//pomijamy element 0 (pusty)
	//	$this->debug('TEST!: count('.count($dzialanie).')');
		$d = count($dzialanie);
		for ($i=1; $i<$d; $i++) {
			$co = explode(': ', $dzialanie[$i]);
			if (count($co)<1) return 0;
			$name = trim(strtolower($co[0]));
			$co[1] = trim($co[1]);
		//	$this->debug('TEST!: name:'.$name);
			switch ($name) {
				case 'when':
				case 'provides':
					$co[1] = explode(' ', $co[1]);
					break;					
			}
			$ret[$name]=$co[1];
			unset($co);
		}
		return $ret;
	}
	public function load_modules($listonly=0, $directory=null) {
		$bad = array('.','..','lang','other');
		$direc = '';
		if ($directory === null)
			$directory = MODULES_DIR;
		else
			$direc = substr($directory, strlen(MODULES_DIR.DIRECTORY_SEPARATOR));
		$dir = scandir($directory);
		if (is_array($dir)) {
			$modules=array();
			foreach ($dir as $v) {
				$name = $v;
				if (is_dir($directory.DIRECTORY_SEPARATOR.$v) && !in_array($v, $bad)) {
					$this->load_modules($listonly, $directory.DIRECTORY_SEPARATOR.$v);
				}
				$v = explode('.', $v);
				if (count($v)>1) {
					if ($v[count($v)-1] == 'php') {
						if (is_array($p = $this->parse(MODULES_DIR.$direc.DIRECTORY_SEPARATOR.$name))) {
							$this->modulesinfo[$p['name']] = array_merge(array('filename' => $direc.DIRECTORY_SEPARATOR.$v[0]), $p);
						}
					}
				}
			}
			return $this->modulesinfo;
		}
	}
}

//mamy nazwe modulu
$mini_bot = new mini_bot();
$modules = $mini_bot->load_modules();
if (isset($_GET['m'])) {
	if ($_GET['m'] == 'bot_core') {
		if (isset($_GET['v'])) {
			$v = explode('.', $_GET['v']);
			$akt = explode('.', BOTVER);
			$ile = count($akt);
			for ($i=0; $i<$ile; $i++) {
				if (!isset($v[$i]))
					exit('0');
				if ($v[$i]<$akt[$i])
					exit('0');
			}
			exit('1');
		} else {
			exit('Aktualna wersja: '.BOTVER);
		}
	}
	if (isset($modules[$_GET['m']])) {
		$mod = $modules[$_GET['m']];
		
		//sprawdzamy czy chce pobrac i reszta pierdol
		if (isset($_GET['a'])) {
			$a = $_GET['a'];
			switch($a) {
				case 'd':
					//przygotowywujemy plik do pobrania
					header('Content-type: text/plain');
					exit(file_get_contents(MODULES_DIR.$mod['filename'].'.php'));
					break;
			}
		}
		
		//mamy wersje modulu
		if (isset($_GET['v'])) {
			$v = explode('.', $_GET['v']);
			$akt = explode('.', $mod['version']);
			$ile = count($akt);
			for ($i=0; $i<$ile; $i++) {
				if (!isset($v[$i]))
					exit('0');
				if ($v[$i]<$akt[$i])
					exit('0');
			}
			exit('1');
		} else { 
			//wysylam aktualna wersje
			if (isset($mod['version']))
				exit('Aktualna wersja: '.$mod['version']);
			exit('1'); //blad
		}
	} else {
		//nie znaleziono modulu
	}
}
?>